/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.ui.async.gui;

import java.io.IOException;

import org.ourgrid.common.ui.InputFieldsUI;
import org.ourgrid.peer.PeerConfiguration;
import org.ourgrid.peer.ui.async.model.PeerAsyncUIModel;

/**
 * Represents a panel where it is possible to define the basic
 * peer settings, like: description and administrator email.
 */
public class BasicPeerConfigurationPanel extends javax.swing.JPanel implements InputFieldsUI {
    
	private static final long serialVersionUID = 1L;

	private PeerAsyncUIModel model;

	/** Creates new form BasicPeerConfigurationPanel 
     * @param model */
    public BasicPeerConfigurationPanel(PeerAsyncUIModel model) {
    	this.model = model;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelLabel = new javax.swing.JLabel();
        descriptionLabel = new javax.swing.JLabel();
        admEmailLabel = new javax.swing.JLabel();
        labelTextField = new javax.swing.JTextField();
        descriptionTextField = new javax.swing.JTextField();
        admEmailTextField = new javax.swing.JTextField();

        labelLabel.setText("Label:");

        descriptionLabel.setText("Description:");

        admEmailLabel.setText("Adm. email:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(admEmailLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(admEmailTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(descriptionLabel)
                            .add(labelLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(labelTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                            .add(descriptionTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE))))
                .addContainerGap(89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(labelLabel)
                    .add(labelTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(descriptionLabel)
                    .add(descriptionTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(admEmailLabel)
                    .add(admEmailTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        initTextFields();
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel admEmailLabel;
    private javax.swing.JTextField admEmailTextField;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextField descriptionTextField;
    private javax.swing.JLabel labelLabel;
    private javax.swing.JTextField labelTextField;
    // End of variables declaration//GEN-END:variables

    /**
     * Disable the editable components of this panel.
     */
    public void disableFieldEdition() {
    	admEmailTextField.setEnabled(false);
		descriptionTextField.setEnabled(false);
		labelTextField.setEnabled(false);
	}
    
	/**
	 * Enable the editable components of this panel.
	 */
    public void enableFieldEdition() {
    	admEmailTextField.setEnabled(true);
		descriptionTextField.setEnabled(true);
		labelTextField.setEnabled(true);
	}
    
	/**
	 * Initializes the textFields of this panel using the default values.
	 */
    private void initTextFields() {
    	admEmailTextField.setText(model.getProperty(PeerConfiguration.PROP_EMAIL));
    	descriptionTextField.setText(model.getProperty(PeerConfiguration.PROP_DESCRIPTION));
    	labelTextField.setText(model.getProperty(PeerConfiguration.PROP_LABEL));
	}
    
	/**
	 * Save the basic properties of the peer.
	 */
    public void saveProperties() {
    	model.setProperty(PeerConfiguration.PROP_EMAIL, admEmailTextField.getText());
    	model.setProperty(PeerConfiguration.PROP_DESCRIPTION, descriptionTextField.getText());
    	model.setProperty(PeerConfiguration.PROP_LABEL, labelTextField.getText());
	}

	/**
	 * Initializes the textFields.
	 */
	public void initFields() throws IOException {
		initTextFields();
	}

	/**
	 * Save the values of the field inputs.
	 */
	public void saveFieldInputs() throws IOException {
		saveProperties();
	}

	/**
	 * Disable the editable components of this panel.
	 */
	public void disableInput() {
		disableFieldEdition();
		
	}

	/**
	 * Enable the editable components of this panel.
	 */
	public void enableInput() {
		enableFieldEdition();
		
	}
    
}
